package com.sunny.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.protocol.a.NativePacketHeader;
import com.sunny.Result;
import com.sunny.dto.ArticleDTO;
import com.sunny.entity.Article;
import com.sunny.service.IArticleService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author sunny
 * @since 2025-10-24
 */
@RestController
@RequestMapping("api")
@AllArgsConstructor
public class ArticleController {
    private final IArticleService articleService;

    /**
     * 新增文章
     */
    @PostMapping("/add")
    public Result<Integer> add(@RequestBody ArticleDTO articleDTO) {
        return Result.success(articleService.add(articleDTO));
    }

    /**
     * 新增文章
     */
    @GetMapping("/view/{articleId}")
    public Result<ArticleDTO> view(@PathVariable Long articleId) {
        return Result.success(articleService.getArticleById(articleId));
    }

    /**
     * 删除文章
     */
    @RequestMapping("/blogs")
    public Result<Page<Article>> blogs(@RequestParam("page") Integer pageNumber, @RequestParam("size") Integer pageSize) {
        Page<Article> articlePage = new Page<>(pageNumber, pageSize);
        articlePage.setCurrent(pageNumber);
        articlePage.setSize(pageSize);
        return Result.success(articleService.blogs(articlePage));
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/delete/{articleId}")
    public Result<Boolean> delete(@PathVariable Long articleId) {
        return Result.success(articleService.deleteArticle(articleId));
    }
}
